<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld"  prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld"  prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
 
<%@ taglib uri="/WEB-INF/tlds/custom-html.tld" prefix="chtml" %>
<%@ page import="java.util.*" %>
<%@ page  import="java.text.SimpleDateFormat" %>
<%@ page import="org.apache.struts.action.Action"%>

<%@ page  import="org.apache.struts.util.RequestUtils" %>
<%@ page  import="com.erp.framework.util.CommonUtil" %>
<%@ page  import="com.erp.app.common.ProjectConstants" %>

<%Locale locale = (Locale)session.getAttribute(Action.LOCALE_KEY);%>
<jsp:useBean id="sendEmailForm" scope="session" type="com.erp.app.sendMail.web.SendEmailForm" />
<%	
//	String pageTitle = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.button.sendEmail", null);
//	String sendLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.label.email.send", null);
//	String cancelLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.button.cancel", null);
//	String fromLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.label.email.from", null);
//	String toLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.label.email.to", null);
//	String subjectLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.label.email.subject", null);
//	String bodyLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.label.email.body", null);
//	String ccLabel = RequestUtils.message(pageContext, "labelResources", Action.LOCALE_KEY, "general.label.email.cc", null);



%>	

<% 	HashMap params = sendEmailForm.getParams();
 
		String languageId = (String) session.getAttribute(ProjectConstants.SESSION_LANGUAGE_ID);
%>

<head>
<title><%//=pageTitle%>Send Email</title>
<link rel="stylesheet" href="<%=request.getContextPath()+ "/css/style.css" %>" type="text/css">
<link rel="stylesheet" href="stylesheet/buttonStyle.css" type="text/css">


<script language="javascript">

var isSubmitted = 0;
 
function doSubmit(event) {
    document.forms[0].action.value = event;
    isSubmitted = 1;
    document.forms[0].submit();
}

function doLoad() {
	<% 
	if (!CommonUtil.isEmpty(sendEmailForm.getAction())) {
		if (sendEmailForm.getAction().equals("sendEmail1")) {
			out.println("window.opener.doSubmit('doNothing');");
			sendEmailForm.setAction("");
		}
 
		if (!sendEmailForm.getAction().equals("selectTemplate"))
			out.println("self.close();");
	
	}
	%>
	window.resizeTo(750, 550);
	window.moveTo(0, 0);
}

function dosend() {
		var sCode = document.getElementById("to");
		var ccCode = document.getElementById("cc");
		var bccCode = document.getElementById("bcc");
        var value = sCode.value;
		var ccvalue = ccCode.value;
		var bccvalue = bccCode.value;
		var emailArray = value.split(",");
		var ccemailArray = ccvalue.split(",");
		var bccemailArray = bccvalue.split(",");
		var emailPat=/^(.+)@(.+)$/;
		var toEmailInvalid = false;
		var ccEmailInvalid = false;
		var bccEmailInvalid = false;
		var msg = "";
		if(sCode == null || value == ""){
			msg += "Please input Receiver's Email Address first";
		}else{
			for(var i = 0; i < emailArray.length; i++){
				var emailValue = emailArray[i];
				var matchArray = emailValue.match(emailPat);
				if (matchArray==null) {
					 toEmailInvalid = true;
					 break;
                 }
			}
		}
		if(ccCode != null && ccvalue != ""){
			for(var i = 0; i < ccemailArray.length; i++){
				var ccemailValue = ccemailArray[i];
				var ccmatchArray = ccemailValue.match(emailPat);
				if (ccmatchArray==null) {
					 ccEmailInvalid = true;
					 break;
                 }
			}
		}
	    if(bccCode != null && bccvalue != ""){
			for(var i = 0; i < bccemailArray.length; i++){
				var bccemailValue = bccemailArray[i];
				var bccmatchArray = bccemailValue.match(emailPat);
				if (bccmatchArray==null) {
					 bccEmailInvalid = true;
					 break;
                 }
			}
		} 
		
		if(toEmailInvalid){
				msg +=  "Receiver's Email Address format is not correct. \n"
		}
		if(ccEmailInvalid){
				msg +=  "CC Email Address format is not correct. \n"
		}
		if(bccEmailInvalid){
				msg +=  "BCC Email Address format is not correct. \n"
		}
		if(msg != ""){
			alert(msg);
		}else{
            doSubmit("sendEmail1");
		}
    }

function doCancel() {
     self.close()
}
</script>
</head>
<body onLoad="javascript:doLoad();" bgcolor="#ffffff">

<html:form action="/sendEmail">
<html:hidden property="action" />
<html:hidden property="eventSrc" />



<table bgcolor="#E0E6E7" border=1 bordercolor="#C1C1C1" cellpadding=0 cellspacing=0 style="font-family: Arial, Helvetica, sans-serif;font-size:9pt;" >	
     <tr valign="top">
		<td bgcolor="#e7e7e7" class="col_label_sendmail"><%//=fromLabel%>From:</td>
		<td class="t2"><bean:write name="sendEmailForm" property="from" filter="true"/>&nbsp;</td>
	 </tr>
	  <tr valign="top">
		<td bgcolor="#e7e7e7" class="col_label_sendmail"><%//=toLabel%>To:</td>
		<td class="t2"><html:text property="to" styleClass="textbox1" maxlength="200" size="50"/></td>
	 </tr>
	 <%if(ProjectConstants.GENERIC_YES.equals(sendEmailForm.getShowCC())){%>
     <tr valign="top">
		<td bgcolor="#e7e7e7" class="col_label_sendmail"><%//=ccLabel%>cc:</td>
		<td class="t2"><html:text property="cc" styleClass="textbox1" maxlength="200" size="50"/></td>
	 </tr>
	 <%}else{%>
     <html:hidden property="cc" value="<%=CommonUtil.nullToString(sendEmailForm.getCc())%>"/>
	 <%}%>
	  <%if(ProjectConstants.GENERIC_YES.equals(sendEmailForm.getShowBCC())){%>
     <tr valign="top">
		<td bgcolor="#e7e7e7" class="col_label_sendmail"><%//=ccLabel%>cc:</td>
		<td class="t2"><html:text property="bcc" styleClass="textbox1" maxlength="200" size="50"/></td>
	 </tr>
	 <%}else{%>
     <html:hidden property="bcc" value="<%=CommonUtil.nullToString(sendEmailForm.getBcc())%>"/>
	 <%}%>
	 <tr valign="top">
		<td bgcolor="#e7e7e7" class="col_label_sendmail"><%//=subjectLabel%>Subject:</td>
		<td class="t2"><html:text property="subject" styleClass="textbox1" maxlength="200" size="50"/></td>
	 </tr>
 
	 
	 <tr valign="top">
			<td bgcolor="#e7e7e7" class="col_label_sendmail"><%//=bodyLabel%>Body:</td>
			<td class="t2"><html:textarea property="body" rows="20" cols="79" styleClass="t2" /></td>
	 </tr>	
	<tr valign="top">
    <td bgcolor="#e7e7e7"></td>
    <td style="padding-bottom:1px;">
		<button name="btn_send" class="commonImagebutton" onClick="javascript:dosend();">
			<div class="commonImagebuttonPanel1" onMouseover="this.className='commonImagebuttonPanel2'" onMouseout="this.className='commonImagebuttonPanel1'">
				<div class="commonImagebuttonPanel2" onMouseover="this.className='commonImagebuttonPanel1'" onMouseout="this.className='commonImagebuttonPanel2'" align="center">
					<%--
						if (locale.equals(Locale.US)) {
					%>
					<div style="padding-top:1px;">
						<%=sendLabel%>
					</div>
					<%} else {%>
					<div style="padding-top:2px;">
						<%=sendLabel%>
					</div>
					<%}--%>Send
				</div>
			</div>
		</button>
		<button name="btn_cancel" class="commonImagebutton" onClick="javascript:doCancel('cancel');">
			<div class="commonImagebuttonPanel1" onMouseover="this.className='commonImagebuttonPanel2'" onMouseout="this.className='commonImagebuttonPanel1'">
				<div class="commonImagebuttonPanel2" onMouseover="this.className='commonImagebuttonPanel1'" onMouseout="this.className='commonImagebuttonPanel2'" align="center">
					<%--
						if (locale.equals(Locale.US)) {
					%>
					<div style="padding-top:1px;">
						<%=cancelLabel%>
					</div>
					<%} else {%>
					<div style="padding-top:2px;">
						<%=cancelLabel%>
					</div>
					<%}--%>Cancel
				</div>
			</div>
		</button>		
    </td>
    </tr>
</table>
</html:form>


